package yrl.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

public class CollectionsDemo {
	static void enumElement(List<String> list) {//遍历
		System.out.println("-----------集合元素如下-----------");
		Iterator<String> iterator=list.iterator();
		while(iterator.hasNext()) {
			System.out.print(iterator.next()+" ");
		}
		System.out.println();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		List<String> list = new ArrayList<>();
		Collections.addAll(list, "1","2","3","1","4","5");
		System.out.println("addAll后");
		enumElement(list);
		
		//用reverse反转
		System.out.println("用reverse反转后：");
		Collections.reverse(list);
		enumElement(list);
		
		//排序
		System.out.println("使用sort()排序后：");
		Collections.sort(list);
		enumElement(list);
		
		//查找元素4
		System.out.println("查找元素4："+(Collections.binarySearch(list, "4")>=0?"YES":"NO"));
		System.out.println("使用repace()替换元素");
		//将1替换为A
		Collections.replaceAll(list, "1", "A");
		enumElement(list);
		
	}

}
